package com.forcadevenda.uteis;

import com.forcadevenda.activity.*;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Vibrator;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageButton;
import java.util.ArrayList;

public class GApi {
    
    public static final String PAGINAPRINCIPAL = "http://www.google.com.br";
    
    public static void vibrar(Context ctx, long tempo){
        Vibrator rr = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);        
        rr.vibrate(tempo);
    }
    
    public static void vibrar(Context ctx){
        vibrar(ctx, 30);
    }    

    public static void navegar(Activity act, String url){
        if (G.strIsEmpty(url))
            url = PAGINAPRINCIPAL;
                
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        act.startActivity(browserIntent);        
    }    
    
    public static void startFala(Activity act){
        try{
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);                
            act.startActivityForResult(intent, G.IDGETVOZ);        
        } catch (ActivityNotFoundException e){
            G.msgErro(act, G.getString(act, R.string.aparelhosemsuporte));
        } catch (Exception e){
            G.msgErro(act, G.getString(act, R.string.errofala), e.getMessage());            
        }
    }        
    
    public static void setBtFala(final Activity act, ImageButton btFala) {
        btFala.setOnClickListener(new android.view.View.OnClickListener() {
            public void onClick(View arg0) {
                startFala(act);
            }
        });
    }
    
    public static String[] getResultFala(Intent data){
        ArrayList<String> resultados = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);                        
        return resultados.toArray(new String[resultados.size()]);
    }    
    
    public static void startCodBarras(Activity act) {
        try{
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);        
            act.startActivityForResult(intent, G.IDGETCODBARRAS);
        } catch (ActivityNotFoundException e){
            G.msgErro(act, G.getString(act, R.string.aparelhosemsuporte));
        } catch (Exception e){
            G.msgErro(act, G.getString(act, R.string.errocodbarras), e.getMessage());            
        }
    }    

    public static void setBtCodBarras(final Activity act, ImageButton btCodBarras) {
        btCodBarras.setOnClickListener(new android.view.View.OnClickListener() {
            public void onClick(View arg0) {
                startCodBarras(act);
            }
        });
    }
    
    public static String getResultCodBarras(Intent data){
        String conteudo = data.getStringExtra("SCAN_RESULT");
        
        if (G.strIsEmpty(conteudo))
            conteudo = "";
        
        return conteudo;
    }    
    
}
